package com.javaee.xiangyang.controller.mall;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.service.AccountService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

@RequestMapping("/MallUser")
@Controller
public class MallUserController {

    @Resource
    AccountService accountService;

    /**
     * 用户注册
     */
    @RequestMapping("/UserRegister")
    public ModelAndView register(HttpSession session, ModelAndView modelAndView, User user) {
        if (accountService.AddUserService(user) == null) {
            //写入数据库
            accountService.AddUserService(user);
            //注册成功  写入信息
            User user1 = accountService.GetUser(user);
            //封入session
            session.setAttribute("user", user1);
            modelAndView.setViewName("redirect:/MallGoods/index");
            return modelAndView;
        }
        modelAndView.addObject("error", accountService.AddUserService(user).getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }

    /**
     * 用户登录
     */
    @RequestMapping("/UserLogin")
    public ModelAndView login(HttpSession session, ModelAndView modelAndView, User user) {
        ServiceResultEnum serviceResultEnum = accountService.userLoginService(user);
        if (serviceResultEnum == ServiceResultEnum.LOGIN_SUCCESS) {
            //登陆成功
            User user1 = accountService.GetUser(user);
            //封入session
            session.setAttribute("user", user1);
            //保存在session
            modelAndView.setViewName("redirect:/MallGoods/index");
            return modelAndView;
        }
        //登陆失败，返回失败信息
        modelAndView.addObject("error", serviceResultEnum.getResult());
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }

    /**
     * 用户修改头像
     */
    @RequestMapping("/UpdateUserHead")
    public ModelAndView updateUserHead(ModelAndView modelAndView, MultipartFile file, HttpSession session) throws IOException {
        User user = (User) session.getAttribute("user");
        if (!Objects.equals(file.getOriginalFilename(), "")) {
            //保存文件
            String filename = file.getOriginalFilename();//这个是文件名称
            file.transferTo(new File("E:\\JSP\\xiangyang\\src\\main\\resources\\static\\Images\\userImages\\" + filename));
            user.setUserHead("\\userImages\\" + filename);
            //修改头像
            ServiceResultEnum serviceResultEnum = accountService.userUpdateHead(user);
            if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
                //重置session
                session.removeAttribute("user");
                session.setAttribute("user", accountService.GetUser(user));
                modelAndView.setViewName("redirect:/MallGoods/index");
                return modelAndView;
            }
            modelAndView.addObject("error", serviceResultEnum.getResult());
        }
        modelAndView.addObject("error", ServiceResultEnum.FILE_FALL);
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }


    /**
     * 用户修改个人信息
     */
    @RequestMapping("/UpdateUser")
    public ModelAndView updateUser(ModelAndView modelAndView, User user, HttpSession session) throws IOException {
        User user1 = (User) session.getAttribute("user");
        user.setUserAccount(user1.getUserAccount());
        //修改信息
        ServiceResultEnum serviceResultEnum = accountService.userUpdateService(user);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            //重置session
            session.removeAttribute("user");
            session.setAttribute("user", accountService.GetUser(user));
            modelAndView.setViewName("redirect:/MallGoods/index");
            return modelAndView;
        }
        modelAndView.addObject("error", ServiceResultEnum.FILE_FALL);
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }


    /**
     * 用户退出登录
     */
    @RequestMapping("/UserLoginOut")
    public ModelAndView loginOut(ModelAndView modelAndView, HttpSession session) {
        session.removeAttribute("user");
        modelAndView.setViewName("redirect:/MallGoods/index");
        return modelAndView;
    }

}
